<template>
    <!-- 引入element-ui 的container布局 -->
    <el-container class="home-container">
        <!-- 头部 -->
        <el-header>
            <div>
                <img src="../assets/logo.jpg" class="logo_img">
                <span>运动管理平台</span>
            </div>
            <el-button type="info" @click="logout">安全退出</el-button>
        </el-header>
        <!-- 主体 -->
        <el-container>
            <!-- 侧边栏 -->
            <el-aside :width="isCollapse?'64px':'200px'">
                <div class="toggle-button" @click="toggleCollapse">|||</div>
                <el-menu unique-opened background-color="#545c64" text-color="#fff" active-text-color="#409eff"
                    :collapse="isCollapse" :collapse-transition="false" :router="true" 
                    :default-active="avtivePath">
                    <!-- 一级菜单 -->
                    <el-submenu :index="item.path" v-for="item,index in menuList" :key="index">
                        <template slot="title">
                            <i :class="iconsObject[item.id]"></i>
                            <span>{{item.title}}</span>
                        </template>

                        <!-- 二级菜单 -->
                        <el-menu-item :index="itm.path" v-for="itm,i in item.sList" :key="i" @click="saveNavState(itm.path)">
                            <template slot="title">
                                <i :class="iconsObject[itm.id]"></i>
                                <span>{{itm.title}}</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>
                </el-menu>
            </el-aside>
            <!-- 主体内容 -->
            <el-main>
                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
</template>

<script>
export default {
    //相当于onload事件
    data(){
        return{
            menuList: [],
            isCollapse: false,
            iconsObject: {
                '100': 'iconfont icon-guanliyuan',
                '200': 'iconfont icon-yundong',
                '101': 'iconfont icon-denglu',
                '102': 'iconfont icon-tianchongxing-',
                '103': 'iconfont icon-yundong',
                '104': 'iconfont icon-shangpin',
                '201': 'iconfont icon-shu',
                '202': 'iconfont icon-huo',
                '203': 'iconfont icon-shiwu',
                '204': 'iconfont icon-denglu',
            },//图标列表
            avtivePath: '/user'//默认路径
        }
    },
    created() {
        //查询menuList，侧边栏的数据
        this.getMenuList();
        this.activePath = window.sessionStorage.getItem('activePath');//每次刷新页面取出上次点击的页面地址
    },
    methods: {
        //退出
        logout() { 
            window.sessionStorage.clear()//清空session
            this.$router.push("/login")
        },
        //请求后台的侧边栏导航数据
        getMenuList() { 
            this.axios.get("/menus").then((res) => { 
                if (res.data.flag != 200) return this.$message.error("获取列表失败")
                this.menuList=res.data.menus
            })
        },
        //控制伸缩
        toggleCollapse() { 
            this.isCollapse=!this.isCollapse
        },
        //保存当前的路径
        saveNavState(activePath) { 
            //存储到session中
            window.sessionStorage.setItem('activePath', activePath)
            this.activePath=activePath//每次子菜单被点击时保存当前路径
        },
    }
}
</script>

<style lang="less">



        .home-container{
            height: 100%;
        }
        .el-header {
            background-color: #373d41;
            display: flex;
            justify-content: space-between; // 左右贴边
            padding:0 !important;
            align-items: center;
            font-size: 20px;
            color: #fff;
            >div{
                display: flex;
                align-items: center;
            }
            span{
               margin:0 15px;
            }
            
        }
        //侧边栏样式
        .el-aside {
            background-color: #333744;
            .el-menu{
                border-right: none;//去除展开时多出的样式
            }
        }
    
        .el-main {
            background-color: #eaedf1;
        }
        .logo_img{
            width: 45px;
            height: 45px;
        }
        .toggle-button {
            background-color: #4A5064;
            font-size: 10px;
            line-height: 24px;
            color: #fff;
            text-align: center;
            letter-spacing: 0.2em;
            cursor: pointer; // 显示鼠标指针为：小手
        }
</style>